package com.demo.dao.imp;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.demo.dao.HibernateBaseDao;
import com.demo.exception.ExceptionMessage;
import com.demo.otherbeans.PagesBean;


public class HibernateBaseDaoImpl extends HibernateDaoSupport implements HibernateBaseDao {
	Logger logger = Logger.getLogger(HibernateBaseDaoImpl.class);
 
	public List findBySql(String sql) {
		SQLQuery sqlQuery = this.getSession().createSQLQuery(sql);
		return sqlQuery.list();
	}

 
	public List getAll(String className) {
		String sql = " from " + className;
		List list = this.getHibernateTemplate().find(sql);
		return list;
	}

 
	public int getCountByHql(String hql) {
		hql  = " select count(*)as c" +hql;
		List l = this.getHibernateTemplate().find(hql);
		return Integer.parseInt(l.get(0).toString());
	}
	
	public int getCountBySql(String sql) {
		List l =  this.findBySql(sql);
		return l.size();
	}
	
	 

	/*
	 * 自定义的密码加密方法
	 * 
	 * @see com.fly.base.util.dao.HibernateFlyDao#XorEnCode(java.lang.String,
	 * java.lang.String)
	 */
	/*
	 * (non-Javadoc)
	 * 
	 * @see com.fly.base.util.dao.HibernateFlyDao#XorEnCode(java.lang.String,
	 * java.lang.String)
	 */
	public String XorEnCode(String key, String S) {
		String Result = "";
		byte[] tmp = S.getBytes();
		byte[] keys = key.getBytes();
		byte c;
		for (int i = 0; i < tmp.length; i++) {
			if (keys.length > 0) {
				c = (byte) (keys[1 + ((i - 1) % keys.length)] ^ tmp[i]);
			} else {
				c = tmp[i];
			}
			Result = Result + Integer.toHexString(c & 0xFF);
		}
		return Result;
	}

	/*
	 * 自定义的密码解密方法
	 * 
	 * @see com.fly.base.util.dao.HibernateFlyDao#XorDeCode(java.lang.String,
	 * java.lang.String)
	 */
	/*
	 * (non-Javadoc)
	 * 
	 * @see com.fly.base.util.dao.HibernateFlyDao#XorDeCode(java.lang.String,
	 * java.lang.String)
	 */
	public String XorDeCode(String key, String S) {
		char c;
		String result;

		result = "";
		byte[] tmp = S.getBytes();
		byte[] a = key.getBytes();

		for (int i = 0; i < tmp.length / 2; i++) {
			String s1 = S.substring(i * 2, i * 2 + 2);
			int ret = Integer.parseInt(s1, 16);
			if (a.length > 0) {
				ret = a[(i % a.length)] ^ ret;
			}
			c = (char) ret;
			result = result + c;
		}
		return result;
	}


 

	 
 

	/*
	 * 执行任意sql语句返回结果集
	 * 
	 * @see com.fly.base.util.dao.HibernateFlyDao#getList(java.lang.String)
	 */
	/*
	 * (non-Javadoc)
	 * 
	 * @see com.fly.base.util.dao.HibernateFlyDao#getList(java.lang.String)
	 */
	public List getList(String sql) {
		List list = this.getHibernateTemplate().find(sql);
		return list;
	}


	public void executeInsOrUpdateSql(String sql) throws SQLException{
		org.hibernate.Session session = this.getHibernateTemplate().getSessionFactory().openSession();  
		Connection ct = session.connection();
		PreparedStatement pstmt = null; 
		pstmt = ct.prepareStatement(sql);
		pstmt.executeUpdate();  
		pstmt.close(); 
		session.close();  
	}


	
	public void saveEntity(Object entity) {
		this.getHibernateTemplate().save(entity);
	}
	


	public void updateEntity(Object entity) {
		this.getHibernateTemplate().update(entity);
	}


	public List findByHql(String hql) {
		return this.getHibernateTemplate().find(hql);
	}


	public PagesBean getPageByHql(String hql, int currentPage, int pageSize,int rowCount) { 
		  PagesBean pagesBean = null;
		  Session session=this.getHibernateTemplate().getSessionFactory().openSession();
		  Query query=session.createQuery(hql);//by hql
		  currentPage = currentPage<0?1:currentPage;
		  pageSize = pageSize<0?10:pageSize;
		  int startRow=(currentPage-1)*pageSize; //分页开始行
		  query.setFirstResult(startRow); 
		  query.setMaxResults(pageSize);
		  List message=query.list();
		  session.close(); 
		  int pageCount = rowCount%pageSize==0?rowCount/pageSize:rowCount/pageSize+1;//总页数
		  pagesBean = new PagesBean();
		  pagesBean.setCurrentPage(currentPage);
		  pagesBean.setPagecount(pageCount);
		  pagesBean.setPagesize(pageSize);
		  pagesBean.setPagecount(rowCount);
		  pagesBean.setPageList(message); 
		  return pagesBean; 
	}


	public PagesBean getPageBySql(String sql, int currentPage, int pageSize,int rowCount) {
		 PagesBean pagesBean = null;
		  Session session=this.getHibernateTemplate().getSessionFactory().openSession();
		  Query query=session.createSQLQuery(sql);//by sql
		  currentPage = currentPage<0?1:currentPage;
		  pageSize = pageSize<0?10:pageSize;
		  int startRow=(currentPage-1)*pageSize; //分页开始行
		  query.setFirstResult(startRow); 
		  query.setMaxResults(pageSize);
		  List message=query.list();
		  session.close(); 
		  int pageCount = rowCount%pageSize==0?rowCount/pageSize:rowCount/pageSize+1;//总页数
		  pagesBean = new PagesBean();
		  pagesBean.setCurrentPage(currentPage);
		  pagesBean.setPagecount(pageCount);
		  pagesBean.setPagesize(pageSize);
		  pagesBean.setPagecount(rowCount);
		  pagesBean.setPageList(message); 
		  return pagesBean; 
	}


	public String getDatabaseType() throws HibernateException, SQLException {
		return this.getHibernateTemplate().getSessionFactory().openSession().connection().getMetaData().getDriverName();
	}
}
